/*
 * @Descripttion: 享元模式
 * @Author: lcx
 * @version: v01
 * @Date: 2023-10-17 23:02:55
 * @LastEditors: lcx
 * @LastEditTime: 2023-10-18 00:16:33
 */

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

abstract class Shape {
    String color;

    abstract void location(int x, int y,int r);
}

class CircleFactory {
    protected Map<String, Shape> map = new HashMap<String, Shape>();

    public Shape getCircle(String color) {
        if ( !map.containsKey(color) ) {
            Shape shape = new Circle(color);
            map.put(color, shape);
            System.out.println("add Circle color: " + color);
        }
        return map.get(color);
    }

}

class Circle extends Shape {

    Circle(String color) {
        this.color = color;
    }

    void location(int x, int y, int r) {
        System.out.println("color: "+ color +" x: " + x + " y: " + y + " r: " + r);
    }
}



class test {
    public static void main(String[] args) {
        CircleFactory circleFactory = new CircleFactory();

        String[] colors = {"red","black","blue","gray","yellow"};
        Random randoms = new Random();

        for (int i = 0; i < 10; i++) {
            Shape shape = circleFactory.getCircle(colors[randoms.nextInt(colors.length)]);
            shape.location(randoms.nextInt(10,20), randoms.nextInt(50,100),randoms. nextInt(200,500));
        }

        // Shape shape1 = circleFactory.getCircle("red");
        // System.out.println(shape1);
        // Shape shape2 = circleFactory.getCircle("green");
        // System.out.println(shape2);
        // Shape shape3 = circleFactory.getCircle("red");
        // System.out.println(shape3);
    }


}